package com.sh.shanhuo.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.BarUtils;
import com.gyf.immersionbar.ImmersionBar;
import com.sh.shanhuo.R;
import com.sh.shanhuo.dialog.LoadingDialog;
import com.tencent.mmkv.MMKV;

public abstract class BaseActivity extends AppCompatActivity implements ClickAction {

    protected abstract int getLayoutId();

    protected void initImmersionBar() {
        ImmersionBar.with(this)
                .statusBarColor(R.color.white)
                .statusBarDarkFont(true)
                .navigationBarColor(R.color.white)
                .navigationBarDarkIcon(true)
                .init();
    }

    protected boolean isUseEventBus() {
        return false;
    }

    protected void initView() {
    }

    protected void initData() {
    }

    /**
     * app切换到后台
     */
    protected void runInBackground() {
    }

    protected BaseActivity mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        Window win = this.getWindow ();
        win.setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
        ActivityManager.getInstance().addActivity(this);
        MMKV.initialize(this);
        initActivity();
    }

    private void initActivity() {
        initLayout();
        initView();
        initData();
    }

    private void initLayout() {
        initImmersionBar();
        if (getLayoutId() > 0) {
            setContentView(getLayoutId());
            initSoftKeyboard();
        }
    }

    private void initSoftKeyboard() {
        // 点击外部隐藏软键盘，提升用户体验
        getContentView().setOnClickListener(v -> hideSoftKeyboard());
    }

    private void hideSoftKeyboard() {
        // 隐藏软键盘，避免软键盘引发的内存泄露
        View view = getCurrentFocus();
        if (view != null) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            if (manager.isActive(view)) {
                manager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    /**
     * 和 setContentView 对应的方法
     */
    protected ViewGroup getContentView() {
        return findViewById(Window.ID_ANDROID_CONTENT);
    }

    @Override
    public void finish() {
        hideSoftKeyboard();
        super.finish();
    }

    /**
     * 如果当前的 Activity（singleTop 启动模式） 被复用时会回调
     */
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        // 设置为当前的 Intent，避免 Activity 被杀死后重启 Intent 还是最原先的那个
        setIntent(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManager.getInstance().removeActivity(this);
    }


    @SuppressLint("SourceLockedOrientationActivity")
    public void activityEnterFullScreen() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //切换横屏
        //获得 WindowManager.LayoutParams 属性对象
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        //直接对它flags变量操作   LayoutParams.FLAG_FULLSCREEN 表示设置全屏
        lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //设置属性
        getWindow().setAttributes(lp);
        //意思大致就是  允许窗口扩展到屏幕之外
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        BarUtils.setNavBarVisibility(this, false);
    }

    @SuppressLint("SourceLockedOrientationActivity")
    public void activityExitFullScreen() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//切换竖屏
        //获得 WindowManager.LayoutParams 属性对象
        WindowManager.LayoutParams lp2 = getWindow().getAttributes();
        //LayoutParams.FLAG_FULLSCREEN 强制屏幕状态条栏弹出
        lp2.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //设置属性
        getWindow().setAttributes(lp2);
        //不允许窗口扩展到屏幕之外  clear掉了
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        BarUtils.setNavBarVisibility(this, true);
    }

    public void onShowLoading() {
        if (!LoadingDialog.with(mContext).isShowing()) {
            LoadingDialog.with(mContext).show(mContext);
        }
    }

    public void onShowTipsLoading(String s) {
        LoadingDialog.with(mContext).setLoadingTxt(s).show(mContext);
    }

    public void onDismissLoading() {
        if (LoadingDialog.with(mContext).isShowing()) {
            LoadingDialog.with(mContext).dismiss();
        }
    }

}
